package com.isoftstone.util;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @描述：导出pdf文件
 * @author：guoX
 * @date：2017/6/7
 */
public class ObjectPdfView extends AbstractPdfView {
    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
        Rectangle rectPageSize = new Rectangle(900,900);// 定义A4页面大小
        document.setPageSize(rectPageSize);
        String title = (String)model.get("title"); // 设置名字
        title+=".pdf";
        List<String> titles = (List<String>) model.get("titles");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment; filename=" + new String(title.getBytes("GB2312"), "ISO_8859_1"));
        document.addTitle(title);
        document.addSubject("export information");
        document.addAuthor("leno");
        document.addCreator("leno");
        document.addKeywords("pdf itext");
        // 定义页头和页尾
        HeaderFooter header = new HeaderFooter(new PdfParagraph(title, 20,
                true), false);
        header.setAlignment(Element.ALIGN_CENTER);
        HeaderFooter footer = new HeaderFooter(new Phrase(
                "This   is   page   "), new Phrase("."));
        footer.setAlignment(Element.ALIGN_CENTER);
        document.setHeader(header);
        document.setFooter(footer);
        document.open();
        PdfPTable table = new PdfPTable(titles.size());
        // table.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.setWidthPercentage(16 * titles.size());
        float[] TableWidths = { 15, 40, 15, 20 };//按百分比分配单元格宽带
        table.setTotalWidth(800);
        table.setLockedWidth(true);
        // 产生表格标题行
        for (int i = 0; i < titles.size(); i++) {
            PdfPCell cell = new PdfPCell(new PdfParagraph(titles.get(i), 14,
                    true));
            cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
            cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
            cell.setBackgroundColor(Color.gray);
            cell.setBorderColor(Color.black);

            table.addCell(cell);
        }

        List<PageData> varList = (List<PageData>) model.get("varList");
        for(int i=0; i<varList.size(); i++){
            PageData vpd = varList.get(i);
            for(int j=0;j<titles.size();j++){
                PdfPCell cell = null;
                String varstr = vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : "";
                if (varstr != null) {
                    cell = new PdfPCell(new PdfParagraph(varstr));
                }
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
                cell.setBorderColor(Color.black);
                table.addCell(cell);
            }


        }
        document.add(table);
        document.close();
    }
}
class PdfParagraph extends Paragraph {

    private static final long serialVersionUID = -244970043180837974L;

    public PdfParagraph(String content) {
        super(content, getChineseFont(12, false));
    }

    public PdfParagraph(String content, int fontSize, boolean isBold) {
        super(content, getChineseFont(fontSize, isBold));
    }

    // 设置字体-返回中文字体
    protected static Font getChineseFont(int nfontsize, boolean isBold) {
        BaseFont bfChinese;
        Font fontChinese = null;
        try {
            bfChinese = BaseFont.createFont("c://windows//fonts//simsun.ttc,1",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            if (isBold) {
                fontChinese = new Font(bfChinese, nfontsize, Font.BOLD);
            } else {
                fontChinese = new Font(bfChinese, nfontsize, Font.NORMAL);
            }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return fontChinese;
    }

    // ת������
    protected Cell ChangeCell(String str, int nfontsize, boolean isBold)
            throws IOException, BadElementException, DocumentException {
        Phrase ph = ChangeChinese(str, nfontsize, isBold);
        Cell cell = new Cell(ph);
        // cell.setBorderWidth(3);

        return cell;
    }

    // ת������
    protected Chunk ChangeChunk(String str, int nfontsize, boolean isBold)
            throws IOException, BadElementException, DocumentException {
        Font FontChinese = getChineseFont(nfontsize, isBold);
        Chunk chunk = new Chunk(str, FontChinese);
        return chunk;
    }

    // ת������
    protected Phrase ChangeChinese(String str, int nfontsize, boolean isBold)
            throws IOException, BadElementException, DocumentException {
        Font FontChinese = getChineseFont(nfontsize, isBold);
        Phrase ph = new Phrase(str, FontChinese);
        return ph;
    }

}